// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import 'bootstrap/dist/css/bootstrap.min.css'
import './lib/mui/css/mui.css'
import VueResource from 'vue-resource'
Vue.use(VueResource)
// import '../static/css/mui.min.css'
//设置请求的根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005';
//全局设置post时候表单数据的组织格式
Vue.http.options.emulateJSON = true
import './lib/mui/js/mui.js'
import Vuex from 'vuex'

Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem('car') || '[]') //读取本地存储的数据
Vue.use(MintUI)
Vue.config.productionTip = false
var store = new Vuex.Store({
  state: {  //this.$store.state.***
    car: car   //购物车数据id,count,price,selected:false
  },
  mutations: {  //this.$store.commit('方法名','按需传递唯一的参数')
   savecar(state,goods){
     //检查本地存储中是否有数据
     var i = false //定义一个标识符默认不存在
     if(car.length == 0){
      state.car.push(goods)
      localStorage.setItem('car', JSON.stringify(car));
     }else if(car.length != 0){
         //遍历看是否有相同的商品
     car.some(item=>{
      if(item.id == goods.id){
        item.count += goods.count
        localStorage.setItem('car', JSON.stringify(car));
        i=true  //当购物车中已经有该商品，把i改为true即存在该商品
      }
    })
    if(i == false){  //如果该购物车中没有找到该商品，则加入购物车
      state.car.push(goods)
      localStorage.setItem('car', JSON.stringify(car));
    }
    } 
   },
   savecount(state,goods){
     //保存购物车中商品数量
     state.car.some(item=>{
       if(item.id == goods.id){
         item.count = goods.count
         localStorage.setItem('car', JSON.stringify(car));
       }
     })

   },
   del(state,id){
     //删除购物车的商品数据
     state.car.some((item,i)=>{
       if(item.id == id){
         state.car.splice(i,1)
         return true
       }
     })
     localStorage.setItem('car', JSON.stringify(car));
   },
   updataselected(state,info){ //保存选中状态
     state.car.forEach(item=>{
       if(item.id == info.id){
         item.selected = info.selected
       }
     })
     localStorage.setItem('car', JSON.stringify(car));
   }

  },
  getters: {  //this.$store.getters.***
    getgoodscount(state){ //定义一个对象，属性名为id，属性值为count
      var o = {}
      state.car.forEach(item=>{
        o[item.id] = item.count
      })
      return o
    },
    getgoodsselected(state){//定义一个对象，属性名为id，属性值为选中状态true/false
      var o = {}
      state.car.forEach(item=>{
        o[item.id] = item.selected
      })
      return o
    },
    getgoodsAll(state){ //计算选中商品总的数量
      var count = 0
      state.car.forEach(item=>{
        if(item.selected == true){
          count += item.count
        }
      })
      return count
    },
    getgoodssum(state){ //计算选中商品总价
      var sum = 0
      state.car.forEach(item=>{
        if(item.selected == true){
          sum += parseInt(item.count) * parseInt(item.price)
        }
      })
      return sum
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  store
})
